﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CommunitySystem.EmpbasicService;

namespace CommunitySystem
{
    public partial class PublicOfficialSelectionWindow : ChildWindow
    {
        private PublicOfficialSelectionService.PublicOfficialSelectionServiceClient Client = new PublicOfficialSelectionService.PublicOfficialSelectionServiceClient();
        PublicOfficialSelectionService.PublicOfficialSelectionModel model = new PublicOfficialSelectionService.PublicOfficialSelectionModel();
        private EmpbasicService.EmpBasicServiceClient Client2 = new EmpbasicService.EmpBasicServiceClient();
        public int ID;
        //public int OrgID;
        public List<string> orgids = new List<string>();
        public bool IsNew = false;
        public string isLock = "";
        public PublicOfficialSelectionWindow(List<string> orgID, bool isnew, int iD,string chakan,string islocked)
        {
            InitializeComponent();
            orgids = orgID;
            ID = iD;
            IsNew = isnew;
            isLock = islocked;
            if (orgids[0] == "0")
            {
                tiaojian.Visibility = System.Windows.Visibility.Collapsed;
                orgid.Visibility = System.Windows.Visibility.Visible;
            }
            else
            {
                tiaojian.Visibility = System.Windows.Visibility.Visible;
                orgid.Visibility = System.Windows.Visibility.Collapsed;
                //Client.GetOrgNamebyOrgIDCompleted += new EventHandler<PublicOfficialSelectionService.GetOrgNamebyOrgIDCompletedEventArgs>(Client_GetOrgNamebyOrgIDCompleted);
                //Client.GetOrgNamebyOrgIDAsync(OrgID);
                string ord = "";
                foreach(string i in orgids)
                {
                    ord = ord + i + ",";
                }
                Client2.GetOrgForHRModelbyIDsCompleted += new EventHandler<EmpbasicService.GetOrgForHRModelbyIDsCompletedEventArgs>(Client2_GetOrgForHRModelbyIDsCompleted);
                Client2.GetOrgForHRModelbyIDsAsync("(" + ord.Substring(0, ord.Length - 1) + ")");
            }
            if (IsNew)
            {
                this.Title = "新增职位信息";
                model = new PublicOfficialSelectionService.PublicOfficialSelectionModel();
                model.OrgID = Convert.ToInt32(orgids[0]);
                orgid.Field = "OrgID";
                model.IsLocked = false;
                this.DataContext = model;
            }
            else
            {
                this.Title = "编辑职位信息";
                if (orgids[0] != "0")
                {
                    model.OrgID = Convert.ToInt32(orgids[0]);
                    orgid.Field = "OrgID";
                }
                
                Client.GetPublicOfficialSelectionCompleted += new EventHandler<PublicOfficialSelectionService.GetPublicOfficialSelectionCompletedEventArgs>(Client_GetPublicOfficialSelectionCompleted);
                Client.GetPublicOfficialSelectionAsync("0", ID, 0, 20, -1, DateTime.Today.Year, isLock,0);
            }
            if(chakan == "查看")
            {
                tiaojian.IsEnabled =
                    orgid.IsEnabled =
                    zhiwei.IsEnabled = renshu.IsEnabled = zhiweijianjie.IsEnabled = wenhuachengdu.IsEnabled =
                                                                                    zhengzhimianmao.IsEnabled =
                                                                                    zige.IsEnabled =
                                                                                    beizhu.IsEnabled =DutyAttribute.IsEnabled = DutyLevel.IsEnabled=
                                                                                    xdjl.IsEnabled = keshi.IsEnabled=
                                                                                    jzxxb.IsEnabled = false;
                OKButton.Visibility = System.Windows.Visibility.Collapsed;
            }
            else
            {
                tiaojian.IsEnabled =
                    orgid.IsEnabled =
                    zhiwei.IsEnabled = renshu.IsEnabled = zhiweijianjie.IsEnabled = wenhuachengdu.IsEnabled =
                                                                                    zhengzhimianmao.IsEnabled =
                                                                                    zige.IsEnabled =
                                                                                    beizhu.IsEnabled = DutyAttribute.IsEnabled = DutyLevel.IsEnabled =
                                                                                    xdjl.IsEnabled = keshi.IsEnabled =
                                                                                    jzxxb.IsEnabled = true;
                OKButton.Visibility = System.Windows.Visibility.Visible;
            }
        }

        void Client2_GetOrgForHRModelbyIDsCompleted(object sender, EmpbasicService.GetOrgForHRModelbyIDsCompletedEventArgs e)
        {
            tiaojian.ItemsSource = e.Result;
            for (int i = 0; i < tiaojian.Items.Count; i++)
            {
                if (((OrgForHRModel)(tiaojian.Items[i])).ID
                    == model.OrgID)
                {
                    tiaojian.SelectedIndex = i;
                }
            }
        }

        void Client_GetOrgNamebyOrgIDCompleted(object sender, PublicOfficialSelectionService.GetOrgNamebyOrgIDCompletedEventArgs e)
        {
            //feiadmin.Text = e.Result;
        }

        void Client_GetPublicOfficialSelectionCompleted(object sender, PublicOfficialSelectionService.GetPublicOfficialSelectionCompletedEventArgs e)
        {
            if (e.Result != null)
                model = e.Result[0];
            if (model.IsJZXXB == true) jzxxb.IsChecked = true;
            if (model.IsXDJL == true) xdjl.IsChecked = true;
            this.DataContext = model;
        }
        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            if (xdjl.IsChecked == true) model.IsXDJL = true; else model.IsXDJL = false;
            if (jzxxb.IsChecked == true) model.IsJZXXB = true; else model.IsJZXXB = false;
            if (model.OrgID == 0)
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择用人部门！");
                return;
            }
            if (tiaojian.Visibility == System.Windows.Visibility.Visible) model.OrgID = Convert.ToInt32(((CommunitySystem.EmpbasicService.OrgForHRModel)(tiaojian.SelectedItem)).ID);
            model.Department = "徐汇区";
            if (IsNew == true)
            {
                model.Year = DateTime.Today.Year;
                Client.AddPublicOfficialSelectionModelCompleted += new EventHandler<PublicOfficialSelectionService.AddPublicOfficialSelectionModelCompletedEventArgs>(Client_AddPublicOfficialSelectionModelCompleted);
                Client.AddPublicOfficialSelectionModelAsync(model);
            }
            else
            {
                Client.UpdatePublicOfficialSelectionModelCompleted += new EventHandler<PublicOfficialSelectionService.UpdatePublicOfficialSelectionModelCompletedEventArgs>(Client_UpdatePublicOfficialSelectionModelCompleted);
                Client.UpdatePublicOfficialSelectionModelAsync(model);
            }
        }

        void Client_UpdatePublicOfficialSelectionModelCompleted(object sender, PublicOfficialSelectionService.UpdatePublicOfficialSelectionModelCompletedEventArgs e)
        {
            if (e.Result == true)
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage("编辑成功！");
                this.DialogResult = true;
            }
        }

        void Client_AddPublicOfficialSelectionModelCompleted(object sender, PublicOfficialSelectionService.AddPublicOfficialSelectionModelCompletedEventArgs e)
        {
            if (e.Result == true)
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage("新增成功！");
                this.DialogResult = true;
            }
        }
    }
}
